package com.ruoyi.order.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.id.SnowflakeIdGenerator;
import com.ruoyi.order.domain.*;
import com.ruoyi.order.enums.OrderStatusEnum;
import com.ruoyi.order.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.MesOrderMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.IdGenerator;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Slf4j
@Service
public class MesOrderServiceImpl implements IMesOrderService {
    @Autowired
    private MesOrderMapper mesOrderMapper;

    @Autowired
    private IMesPalletService mesPalletService;
    @Autowired
    private IMesOrderPalletRecordService mesOrderPalletRecordService;

    @Autowired
    private IMesWarehousePositionService mesWarehousePositionService;

    @Autowired
    private IMesOrderPositionRecordService mesOrderPositionRecordService;

    @Autowired
    private IMesOrderProductService mesOrderProductService;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MesOrder selectMesOrderById(Long id) {
        MesOrder mesOrder = mesOrderMapper.selectMesOrderById(id);
        return mesOrder;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param mesOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MesOrder> selectMesOrderList(MesOrder mesOrder) {
        return mesOrderMapper.selectMesOrderList(mesOrder);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param mesOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMesOrder(MesOrder mesOrder) {
        mesOrder.setCreateTime(DateUtils.getNowDate());
        return mesOrderMapper.insertMesOrder(mesOrder);
    }


    /**
     * 修改【请填写功能名称】
     *
     * @param mesOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMesOrder(MesOrder mesOrder) {
        MesOrder oldMesOrder = mesOrderMapper.selectMesOrderById(mesOrder.getId());
        Set<String> oldPalletCodeSet =  new HashSet<>();
        Set<String> newPalletCodeSet =  new HashSet<>();



        if(StringUtils.isNotEmpty(oldMesOrder.getPalletCode())){
            String[] oldPalletCodeArray = oldMesOrder.getPalletCode().split(",");
            for (String oldPalletCode : oldPalletCodeArray){
                oldPalletCodeSet.add(oldPalletCode);
            }
        }
        if(StringUtils.isNotEmpty(mesOrder.getPalletCode())){
            String[] newPalletCodeArray = mesOrder.getPalletCode().split(",");
            for (String newPalletCode : newPalletCodeArray){
                newPalletCodeSet.add(newPalletCode);
            }
        }

        //释放托盘
        for (String oldPalletCode : oldPalletCodeSet){
            if (!newPalletCodeSet.contains(oldPalletCode)){
                MesPallet pallet = mesPalletService.selectMesPalletByCode(oldPalletCode);
                pallet.setUpdateTime(new Date());
                pallet.setStatus("Y");//空闲状态
                mesPalletService.updateMesPallet(pallet);
                MesOrderPalletRecord oldParam = new MesOrderPalletRecord();
                oldParam.setOrderId(mesOrder.getId());
                oldParam.setPalletCode(oldPalletCode);
                //更新占用托盘记录
                List<MesOrderPalletRecord> oldRecordList = mesOrderPalletRecordService.selectMesOrderPalletRecordList(oldParam);
                for (MesOrderPalletRecord mesOrderPalletRecord : oldRecordList){
                    mesOrderPalletRecord.setUpdateTime(new Date());
                    mesOrderPalletRecord.setEndTime(DateUtils.getNowDate());
                    mesOrderPalletRecordService.updateMesOrderPalletRecord(mesOrderPalletRecord);
                }
            }
        }
        //占用托盘
        for (String newPalletCode : newPalletCodeSet){
            if (!oldPalletCodeSet.contains(newPalletCode)){
                MesPallet pallet = mesPalletService.selectMesPalletByCode(newPalletCode);
                pallet.setUpdateTime(new Date());
                pallet.setStatus("N");//非空闲状态
                mesPalletService.updateMesPallet(pallet);
                //生成占用托盘记录
                MesOrderPalletRecord mesOrderPalletRecord = new MesOrderPalletRecord();
                mesOrderPalletRecord.setPalletCode(newPalletCode);
                mesOrderPalletRecord.setOrderId(mesOrder.getId());
                mesOrderPalletRecord.setOrderNo(mesOrder.getOrderNo());
                mesOrderPalletRecord.setCustomerName(mesOrder.getCustomerName());
                mesOrderPalletRecord.setModel(mesOrder.getModel());
                mesOrderPalletRecord.setCreator(SecurityUtils.getUsername());
                mesOrderPalletRecord.setBeginTime(DateUtils.getNowDate());
                mesOrderPalletRecord.setCreateTime(DateUtils.getNowDate());
                mesOrderPalletRecordService.insertMesOrderPalletRecord(mesOrderPalletRecord);
            }
        }
        Set<String> oldPositionCodeSet =  new HashSet<>();
        Set<String> newPositionCodeSet =  new HashSet<>();

        if(StringUtils.isNotEmpty(oldMesOrder.getPositionCode())){
            String[] oldPositionCodeArray = oldMesOrder.getPositionCode().split(",");
            for (String oldPositionCode : oldPositionCodeArray){
                oldPositionCodeSet.add(oldPositionCode);
            }
        }
        if(ArrayUtils.isNotEmpty(mesOrder.getPositionCodes())){
            mesOrder.setPositionCode(StringUtils.join(mesOrder.getPositionCodes(), ","));
            for (String newPositionCode : mesOrder.getPositionCodes()){
                newPositionCodeSet.add(newPositionCode);
            }
        }
        //释放仓位
        for (String oldPositionCode : oldPositionCodeSet){
            if (!newPositionCodeSet.contains(oldPositionCode)){
                MesWarehousePosition position = mesWarehousePositionService.selectMesWarehousePositionByCode(oldPositionCode);
                position.setUpdateTime(new Date());
                position.setStatus("Y");//空闲状态
                mesWarehousePositionService.updateMesWarehousePosition(position);
                MesOrderPositionRecord oldParam = new MesOrderPositionRecord();
                oldParam.setOrderId(mesOrder.getId());
                oldParam.setPositionCode(oldPositionCode);
                //更新仓位占用记录
                List<MesOrderPositionRecord> oldRecordList = mesOrderPositionRecordService.selectMesOrderPositionRecordList(oldParam);
                for (MesOrderPositionRecord mesOrderPositionRecord : oldRecordList){
                    mesOrderPositionRecord.setUpdateTime(new Date());
                    mesOrderPositionRecord.setEndTime(DateUtils.getNowDate());
                    mesOrderPositionRecordService.updateMesOrderPositionRecord(mesOrderPositionRecord);
                }
            }
        }
        //占用仓位
        for (String newPositionCode : newPositionCodeSet){
            if (!oldPositionCodeSet.contains(newPositionCode)){
                MesWarehousePosition postion = mesWarehousePositionService.selectMesWarehousePositionByCode(newPositionCode);
                postion.setUpdateTime(new Date());
                postion.setStatus("N");//非空闲状态
                mesWarehousePositionService.updateMesWarehousePosition(postion);
                //生成占用托盘记录
                MesOrderPositionRecord positionRecord =  new MesOrderPositionRecord();
                positionRecord.setPalletCode(newPositionCode);
                positionRecord.setOrderId(mesOrder.getId());
                positionRecord.setOrderNo(mesOrder.getOrderNo());
                positionRecord.setCustomerName(mesOrder.getCustomerName());
                positionRecord.setModel(mesOrder.getModel());
                positionRecord.setCreator(SecurityUtils.getUsername());
                positionRecord.setCreateTime(DateUtils.getNowDate());
                positionRecord.setBeginTime(DateUtils.getNowDate());
                mesOrderPositionRecordService.insertMesOrderPositionRecord(positionRecord);
            }
        }

        mesOrder.setUpdateTime(DateUtils.getNowDate());
        mesOrder.setUpdater(SecurityUtils.getUsername());
        mesOrder.setStatus(OrderStatusEnum.BEFORE_TEST.getCode());
        log.info("修改订单状态：{}", JSON.toJSONString(mesOrder));
        return mesOrderMapper.updateMesOrder(mesOrder);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMesOrderByIds(Long[] ids) {
        return mesOrderMapper.deleteMesOrderByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMesOrderById(Long id) {
        return mesOrderMapper.deleteMesOrderById(id);
    }
}
